package com.bz.xtcx.manager.mapper;

import com.bz.xtcx.manager.entity.LibServices;
import com.bz.xtcx.manager.mapper.provider.LibServiceProvider;
import com.bz.xtcx.manager.persist.conditions.LibCondition;
import com.bz.xtcx.manager.vo.VoQuery;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.StatementType;

import java.util.List;

public interface LibServiceMapper {

	@Insert("insert into `lib_service`(id, number, credit_level, user_id, form_id, name, org_code, "
            + " country,address,zip_code,org_nature, org_nature_other,org_type, org_type_other, "
            + " linkman,lxzw,lxmobile,lxphone,lxemail,lpname,lpphone,lpmobile,lpemail,"
            + " service_amount_last, service_amount_before, service_amount_previous,"
			+ " service_quantity_last, service_quantity_before, service_quantity_previous,"
            + " personnel_number, undergraduate_number,middle_title_number,project,honor,work_result, "
            + " services_provided, search_param, status, creater) "
		    + " VALUES(#{id, jdbcType=VARCHAR},"
		    + " #{number, jdbcType=VARCHAR},"
		    + " #{creditLevel, jdbcType=INTEGER},"
		    + " #{userId, jdbcType=VARCHAR},"
		    + " #{formId, jdbcType=VARCHAR},"
		    + " #{name, jdbcType=VARCHAR},"
		    + " #{code, jdbcType=VARCHAR},"
            + " #{country, jdbcType=VARCHAR},"
            + " #{address, jdbcType=VARCHAR},"
            + " #{zipCode, jdbcType=VARCHAR},"
            + " #{orgNature, jdbcType=VARCHAR},"
            + " #{orgNatureOther, jdbcType=VARCHAR},"
		    + " #{orgType, jdbcType=VARCHAR},"
            + " #{orgTypeOther, jdbcType=VARCHAR},"
		    + " #{linkman, jdbcType=VARCHAR},"
            + " #{lxzw, jdbcType=VARCHAR},"
            + " #{lxmobile, jdbcType=VARCHAR},"
            + " #{lxphone, jdbcType=VARCHAR},"
            + " #{lxemail, jdbcType=VARCHAR},"
            + " #{lpname, jdbcType=VARCHAR},"
            + " #{lpphone, jdbcType=VARCHAR},"
            + " #{lpmobile, jdbcType=VARCHAR},"
            + " #{lpemail, jdbcType=VARCHAR},"
		    + " #{serviceAmountLast, jdbcType=VARCHAR},"
		    + " #{serviceAmountBefore, jdbcType=VARCHAR},"
		    + " #{serviceAmountPrevious, jdbcType=VARCHAR},"
		    + " #{serviceQuantityLast, jdbcType=VARCHAR},"
		    + " #{serviceQuantityBefore, jdbcType=VARCHAR},"
		    + " #{serviceQuantityPrevious, jdbcType=VARCHAR},"
            + " #{personnelNumber, jdbcType=VARCHAR},"
            + " #{undergraduateNumber, jdbcType=VARCHAR},"
            + " #{middleTitleNumber, jdbcType=VARCHAR},"
            + " #{project, jdbcType=VARCHAR},"
            + " #{honor, jdbcType=VARCHAR},"
            + " #{workResult, jdbcType=VARCHAR},"
		    + " #{servicesProvided, jdbcType=VARCHAR},"
			+ " #{searchParam, jdbcType=VARCHAR},"
		    + " #{status, jdbcType=INTEGER},"
		    + " #{creater, jdbcType=VARCHAR})"
		    )
    @SelectKey(before = true, keyProperty = "id", resultType = String.class, statementType = StatementType.STATEMENT, statement="select uuid()")
	int insert(LibServices e);
	
	@Delete("delete from `lib_service` where id = #{id}")
	int del(String id);
	
	@Update("update `lib_service` set "
            + " name=#{name, jdbcType=VARCHAR},"
			+ " credit_level=#{creditLevel, jdbcType=VARCHAR},"
			+ " org_code=#{code, jdbcType=VARCHAR},"
            + " country=#{country, jdbcType=VARCHAR},"
            + " address=#{address, jdbcType=VARCHAR},"
            + " zip_code=#{zipCode, jdbcType=VARCHAR},"
            + " org_nature=#{orgNature, jdbcType=VARCHAR},"
            + " org_nature_other=#{orgNatureOther, jdbcType=VARCHAR},"
			+ " org_type=#{orgType, jdbcType=VARCHAR},"
            + " org_type_other=#{orgTypeOther, jdbcType=VARCHAR},"
			+ " linkman=#{linkman, jdbcType=VARCHAR},"
            + " lxzw=#{lxzw, jdbcType=VARCHAR},"
            + " lxmobile=#{lxmobile, jdbcType=VARCHAR},"
            + " lxphone=#{lxphone, jdbcType=VARCHAR},"
            + " lxemail=#{lxemail, jdbcType=VARCHAR},"
            + " lpname=#{lpname, jdbcType=VARCHAR},"
            + " lpphone=#{lpphone, jdbcType=VARCHAR},"
            + " lpmobile=#{lpmobile, jdbcType=VARCHAR},"
            + " lpemail=#{lpemail, jdbcType=VARCHAR},"
			+ " service_amount_last=#{serviceAmountLast, jdbcType=VARCHAR},"
			+ " service_amount_before=#{serviceAmountBefore, jdbcType=VARCHAR},"
			+ " service_amount_previous=#{serviceAmountPrevious, jdbcType=VARCHAR},"
			+ " service_quantity_last=#{serviceQuantityLast, jdbcType=VARCHAR},"
			+ " service_quantity_before=#{serviceQuantityBefore, jdbcType=VARCHAR},"
			+ " service_quantity_previous=#{serviceQuantityPrevious, jdbcType=VARCHAR},"
            + " personnel_number=#{personnelNumber, jdbcType=VARCHAR},"
            + " undergraduate_number=#{undergraduateNumber, jdbcType=VARCHAR},"
            + " middle_title_number=#{middleTitleNumber, jdbcType=VARCHAR},"
            + " project=#{project, jdbcType=VARCHAR},"
            + " honor=#{honor, jdbcType=VARCHAR},"
            + " work_result=#{workResult, jdbcType=VARCHAR},"
			+ " services_provided=#{servicesProvided, jdbcType=VARCHAR},"
            + " search_param=#{searchParam, jdbcType=VARCHAR},"
			+ " status=#{status, jdbcType=INTEGER},"
			+ " updater=#{updater, jdbcType=VARCHAR}"
			+ " where id=#{id}")
	int update(LibServices e);
	
	@Update("update `lib_service` set status=#{status, jdbcType=INTEGER} where id=#{id}")
	int updateStatus(@Param("id")String id, @Param("status")int status);
	
	@Update("update `lib_service` set credit_level=#{creditLevel, jdbcType=INTEGER} where id=#{id}")
	int updateCreditLevel(@Param("id")String id, @Param("creditLevel")int creditLevel);
	
	@SelectProvider(type = LibServiceProvider.class, method = "findByCondition")
	@Results(
		id = "libService",
		value = {
		    @Result(id = true, property = "id", column = "id"),
		    @Result(property = "number", column = "number"),
		    @Result(property = "creditLevel", column = "credit_level"),
		    @Result(property = "userId", column = "user_id"),
		    @Result(property = "name", column = "name"),
		    @Result(property = "code", column = "org_code"),
            @Result(property = "country", column = "country"),
            @Result(property = "address", column = "address"),
            @Result(property = "zipCode", column = "zip_code"),
            @Result(property = "orgNature", column = "org_nature"),
            @Result(property = "orgNatureOther", column = "org_nature_other"),
		    @Result(property = "orgType", column = "org_type"),
		    @Result(property = "orgTypeOther", column = "org_type_other"),
		    @Result(property = "linkman", column = "linkman"),
            @Result(property = "lxzw", column = "lxzw"),
            @Result(property = "lxmobile", column = "lxmobile"),
            @Result(property = "lxphone", column = "lxphone"),
            @Result(property = "lxemail", column = "lxemail"),
            @Result(property = "lpname", column = "lpname"),
            @Result(property = "lpphone", column = "lpphone"),
            @Result(property = "lpmobile", column = "lpmobile"),
            @Result(property = "lpemail", column = "lpemail"),
		    @Result(property = "serviceAmountLast", column = "service_amount_last"),
		    @Result(property = "serviceAmountBefore", column = "service_amount_before"),
		    @Result(property = "serviceAmountPrevious", column = "service_amount_previous"),
		    @Result(property = "serviceQuantityLast", column = "service_quantity_last"),
		    @Result(property = "serviceQuantityBefore", column = "service_quantity_before"),
		    @Result(property = "serviceQuantityPrevious", column = "service_quantity_previous"),
            @Result(property = "personnelNumber", column = "personnel_number"),
            @Result(property = "undergraduateNumber", column = "undergraduate_number"),
            @Result(property = "middleTitleNumber", column = "middle_title_number"),
            @Result(property = "project", column = "project"),
            @Result(property = "honor", column = "honor"),
            @Result(property = "workResult", column = "work_result"),
		    @Result(property = "servicesProvided", column = "services_provided"),
		    @Result(property = "form", column = "form_id", one = @One(select = "com.bz.xtcx.manager.mapper.BusUserFormHisMapper.findById") ),
		    @Result(property = "logo", column = "id", one = @One(select = "com.bz.xtcx.manager.mapper.LibImageMapper.findLogoByLibId") ),
		    @Result(property = "status", column = "status"),
            @Result(property = "searchParam", column = "search_param"),
		    @Result(property = "creater", column = "creater"),
		    @Result(property = "createTime", column = "create_time"),
		    @Result(property = "updater", column = "updater"),
		    @Result(property = "updateTime", column = "update_time")
	    }
	)
    List<LibServices> findByCondition(VoQuery e);
	
	@SelectProvider(type = LibServiceProvider.class, method = "findByConditionApi")
    List<LibServices> findByConditionApi(LibCondition e);
	
	@Select("select * from `lib_service` where user_id = #{userId}")
	@ResultMap("libService")
	LibServices findByUserId(String userId);
	
	@Select("select * from `lib_service` where name = #{name} and org_code = #{code} limit 1")
	@ResultMap("libService")
	LibServices findByNameAndCode(@Param("name")String name, @Param("code")String code);
	
	@Select("select * from `lib_service` where id = #{id}")
	@ResultMap("libService")
	LibServices findById(String id);
	
	@Select("select * from `lib_service` where form_id = #{id}")
	@ResultMap("libService")
	LibServices findByFormId(String id);
}
